home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Function Prototype placement in source files
- Date: Mon, 19 Feb 1996 15:38:59 +0200
- Organization: Carelcomp Forest
- Message-ID: <31287D73.5B18@cmt.lpr.mail.carel.fi>
- References: <4g0obg$78b@barad-dur.nas.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Matt Scott wrote:
- >
- > I have a question of style for everyone knowledgable and interested
- > in the subject.
- >
- > My C Programming prof. at the community college, as well as the book
- > he teaches with, put function prototypes inside main(). I didn't write
- > it down or print it out, but here is what I remember of an example he
- > wrote in the past to illustrate functions and static variables:
- > [snip]
- > In almost every instance of prototypes I've seen, they are declared
- > outside of any function -- mostly above all other functions, and in
- > a header file most often.
- >
- > Is there a good reason to do it the way he and the book do it?
- >
- > Thanks in advance for any thoughtful answers.
-
- Placing function prototypes inside a function body was once a common practice,
- but now when there are standard header files, this should be avoided. Functions
- declared inside a function body are said to have a 'function scope', ie. they
- are properly accessible from within the function declaring them only.
- Furthermore, in your example, you had to declare 'float one(void)' twice, once
- in 'main()' and then in 'two()'. This increases probability of errors. Consider,
- what would happen in a medium-size commercial product program, using strcpy,
- malloc, etc all over the functions. Declaring prototypes in function scope would
- be a very poor practice... And, what if your function 'one()' changed from
- 'float one(void)' to 'double one(int)' ? You'd have to go over your whole bunch
- of functions in order to make it work... Too much work involved, it seems. Just
- stick those prototypes in a header file and #include it. Maybe your professor
- hasn't quite got it with C... :)
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-